package app.android.alampang.utility;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import app.android.alampang.model.Note;

/**
 * Created by Supanut on 2/15/2015.
 */
public class DatabaseHandler extends SQLiteOpenHelper
{
    private static final String LOG = DatabaseErrorHandler.class.getName();

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "alampang_db";

    ////////////////////// TABLE NOTE ////////////////////////
    private static final String TABLE_NOTE = "note";

    private static final String NOTE_ID = "id";
    private static final String NOTE_NAME = "name";
    private static final String NOTE_DESCRIPTION = "description";
    private static final String NOTE_IMG_PATH = "imagepath";
    /////////////////////////////////////////////////////////


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE "+TABLE_NOTE+" ("+NOTE_ID+" INTEGER PRIMARY KEY,"
                                                    +NOTE_NAME + " VARCHAR(50),"
                                                    +NOTE_DESCRIPTION + " TEXT,"
                                                    +NOTE_IMG_PATH + " VARCHAR(50))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_NOTE;
        db.execSQL(sql);

        onCreate(db);
    }

    public void close()
    {
        SQLiteDatabase db = this.getReadableDatabase();

        if (db != null && db.isOpen())
            db.close();
    }

    public void insert(Note note)
    {
        SQLiteDatabase db = this.getReadableDatabase();

        ContentValues values = new ContentValues();
        values.put(NOTE_NAME, note.getName());
        values.put(NOTE_DESCRIPTION, note.getDescription());
        values.put(NOTE_IMG_PATH, note.getImageUri());

        long oId = db.insert(TABLE_NOTE, null, values);
 note.setId((int)oId);

        db.close();
    }

    public void delete(Note note)
    {
        String sql = "DELETE FROM " + TABLE_NOTE + " WHERE " + NOTE_ID + " = " + note.getId();

        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL(sql);
        db.close();
    }

    public void update(Note note)
    {
        String sql = "UPDATE " + TABLE_NOTE + " SET ";
               sql+= NOTE_NAME + " = '"+note.getName()+"',";
               sql+= NOTE_DESCRIPTION + " = '"+note.getDescription()+"', ";
               sql+= NOTE_IMG_PATH + " = '"+note.getImageUri()+"' ";
               sql+= "WHERE " + NOTE_ID + " = " + note.getId();

        //Log.e("DATABASE", sql);
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL(sql);
        db.close();
    }

    public List<Note> getAllNotes()
    {
        List<Note> notes = new ArrayList<>();

        String sql = "SELECT * FROM " + TABLE_NOTE;

        Log.e(LOG, sql);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor reader = db.rawQuery(sql, null);

        if (reader.moveToFirst())
        {
            do
            {
                Note note = new Note();
                note.setId(reader.getInt(reader.getColumnIndex(NOTE_ID)));
                note.setName(reader.getString(reader.getColumnIndex(NOTE_NAME)));
                note.setDescription(reader.getString(reader.getColumnIndex(NOTE_DESCRIPTION)));
                note.setImageUri(reader.getString(reader.getColumnIndex(NOTE_IMG_PATH)));

                notes.add(note);
            }while(reader.moveToNext());

        }
        return notes;
    }
}
